package com.cqrcb.cloud.service.quota;

import com.alibaba.fastjson.JSON;
import com.cqrcb.cloud.entity.api.req.RequestHeader;
import com.cqrcb.cloud.entity.constant.MarketConstant;
import com.cqrcb.cloud.entity.quota.CustomerMultiQuotaCtrlDetail;
import com.cqrcb.cloud.entity.quota.MultiQuotaCtrlMarketMain;
import com.cqrcb.cloud.entity.quota.MultiQuotaLog;
import com.cqrcb.cloud.entity.quota.io.MultiQuotaCtrlMarketMainIO;
import com.cqrcb.cloud.entity.quota.io.base.QuotaBaseIO;
import com.cqrcb.cloud.mapper.quota.MultiQuotaLogMapper;
import com.cqrcb.cloud.utils.DateUtils;
import com.cqrcb.cloud.utils.IPUtils;
import com.cqrcb.cloud.utils.SnowFlakeUtils;
import com.github.pagehelper.PageInfo;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* MultiQuotaLogImpl
*
* @author 系统
* @date 2021-06-01 11:25:18
*/
@Service
public class MultiQuotaLogImpl implements MultiQuotaLogService {
    @Autowired
    private MultiQuotaLogMapper multiQuotaLogMapper;

    @Override
    public int insert(MultiQuotaLog record) {
        return multiQuotaLogMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(MultiQuotaLog record) {
        return multiQuotaLogMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public MultiQuotaLog buildMainLogAndSaveDB(RequestHeader header , String type, QuotaBaseIO baseIO, Object beforeObj){
        MultiQuotaLog multiQuotaLog = MultiQuotaLog.builder()
                .id(SnowFlakeUtils.getId())
                .sid(header.getSid())
                .type(type)
                .ipAddr(IPUtils.getLocalIP())
                .traceId(MDC.get(MarketConstant.traceId))
                .skyTraceId(TraceContext.traceId())
                .dataId(baseIO.getDataId())
                .beforeQuota(JSON.toJSONString(beforeObj))
                .dataType(baseIO.getDataType())
                .useQuota(baseIO.getUseQuota())
                .quotaType(baseIO.getQuotaType())
                .param(JSON.toJSONString(baseIO))
                .status(0)
                .createDate(DateUtils.getNow())
                .createTime(DateUtils.getNow()).build();
        insert(multiQuotaLog);
        return multiQuotaLog;
    }

    @Override
    public void updateMultiQuotaLog(MultiQuotaLog log, Object t, int status, String statusDesc){
        log.setStatus(status);
        log.setStatusDesc(statusDesc);
        log.setAfterQuota(JSON.toJSONString(t));
        updateByPrimaryKeySelective(log);
    }

}
